package com.dawnling.androidutils.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.dawnling.androidutils.R;

/**
 * 自定义进度分割线
 */
public class KeytopProgressView extends View {

    private Context mContext;
    private static int margin = 10;
    private static int centerWhite = 25;//中间分割部分的宽度
    private static int centerAngle = 15;//中间分割部分的倾斜度

    /**
     * 分段颜色
     */
    private static int[] SECTION_COLORS = new int[]{Color.GREEN, Color.RED};//左边颜色
    private static int[] SECTION_COLORS_RIGHT = new int[]{Color.GREEN, Color.RED};//右边颜色

    /**
     * 默认宽度
     */
    private int mWidth = 500;

    /**
     * 默认高度
     */
    private int mHeight = 90;

    /**
     * 设置最大范围
     */
    private float maxCount = 100f;

    /**
     * 设置当前刻度
     */
    private float currentCount = 50f;

    /**
     * 当前刻度位置
     */
    private float selction;

    /**
     * 主画笔，画刻度尺
     */
    private Paint mPaint;//左边画笔
    private Paint mPaintRight;//右边画笔

    /**
     * 文字画笔
     */
    private Paint textPaint;
    private Paint centerPaint;//中间分割段画笔

    /**
     * 画圆柱
     */
    private RectF rectProgressLeft;//左边圆柱
    private RectF rectProgressRight;//右边圆柱
    private LinearGradient shader;//左边渐变色
    private LinearGradient shaderRight;//右边渐变色

    /**
     * 圆角矩形的高度
     */
    private int mDefaultTempHeight = dipToPx(60);
    /**
     * 默认字体大小
     */
    private int mDefaultTextSize = 30;
    private int textSpace = dipToPx(5);

    public KeytopProgressView(Context context) {
        super(context);
        init(context);
    }

    public KeytopProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public KeytopProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化各种画笔
     * @param context
     */
    private void init(Context context) {
        this.mContext = context;
        //圆角矩形paint
        mPaint = new Paint();
        //防止边缘的锯齿
        mPaint.setAntiAlias(true);
        //圆角矩形paint
        mPaintRight = new Paint();
        //防止边缘的锯齿
        mPaintRight.setAntiAlias(true);

        //文本paint
        textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(mDefaultTextSize);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setColor(mContext.getResources().getColor(R.color.colorAccent));
        //中间分割画笔
        centerPaint = new TextPaint();
        centerPaint.setAntiAlias(true);
        centerPaint.setTextAlign(Paint.Align.CENTER);
        centerPaint.setColor(mContext.getResources().getColor(android.R.color.white));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        mHeight = mDefaultTextSize + mDefaultTempHeight + textSpace;
        if (widthSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(mWidth, mHeight);
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(mWidth, heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, mHeight);
        }else setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //左边宽
        int leftWhite = (int)((currentCount/100) * mWidth);
        //右边宽
        int rightWhite = mWidth - (int)((currentCount/100) * mWidth);

        final int paddingLeft = getPaddingLeft();
        final int paddingBottom = getPaddingBottom();

        //确定圆角矩形的范围,在TmepView的最底部,top位置为总高度-圆角矩形的高度
        rectProgressLeft = new RectF();
        rectProgressLeft.left = 0 + paddingLeft;
        rectProgressLeft.top = mHeight - mDefaultTempHeight;
        rectProgressLeft.right = leftWhite + margin;
        rectProgressLeft.bottom = mHeight - paddingBottom;
        //确定圆角矩形的范围,在TmepView的最底部,top位置为总高度-圆角矩形的高度
        rectProgressRight = new RectF();
        rectProgressRight.left = leftWhite - margin;
        rectProgressRight.top = mHeight - mDefaultTempHeight;
        rectProgressRight.right = leftWhite + rightWhite;
        rectProgressRight.bottom = mHeight - paddingBottom;

        SECTION_COLORS = new int[]{mContext.getResources().getColor(R.color.colorOrange), mContext.getResources().getColor(R.color.colorOrangeLight)};
        SECTION_COLORS_RIGHT = new int[]{mContext.getResources().getColor(R.color.colorBlue), mContext.getResources().getColor(R.color.colorBlueLight)};
        shader = new LinearGradient(0, mHeight - mDefaultTempHeight, leftWhite, mHeight, SECTION_COLORS, null, Shader.TileMode.MIRROR);
        shaderRight = new LinearGradient(mWidth, mHeight, rightWhite, mHeight - mDefaultTempHeight, SECTION_COLORS_RIGHT, null, Shader.TileMode.MIRROR);
        mPaint.setShader(shader);
        mPaintRight.setShader(shaderRight);
        //当前位置占比
        selction = currentCount / maxCount;
        //绘制圆角矩形 mDefaultTempHeight / 2确定圆角的圆心位置
        if (leftWhite != 0 && rightWhite != 0){
            canvas.drawRoundRect(rectProgressLeft, mDefaultTempHeight / 2, mDefaultTempHeight / 2, mPaint);
            canvas.drawRoundRect(rectProgressRight, mDefaultTempHeight / 2, mDefaultTempHeight / 2, mPaintRight);
            //画图片，就是贴图
//            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.img_btn_delete);
//            canvas.drawBitmap(bitmap, mWidth * selction - 18, mHeight - mDefaultTempHeight, mPaint);
            //画中间白色菱形
            Path iPath = new Path();
            iPath.moveTo(leftWhite - (centerWhite/2) - margin, mHeight);//左下角 190
            iPath.lineTo(leftWhite - (centerWhite/2) + centerWhite - margin, mHeight);//右下角 210
            iPath.lineTo(leftWhite - (centerWhite/2) + centerWhite + centerAngle - margin, mHeight - mDefaultTempHeight);//右上角 225
            iPath.lineTo(leftWhite - (centerWhite/2) + centerAngle - margin, mHeight - mDefaultTempHeight);//左上角 205
            iPath.close();
            canvas.drawPath(iPath, centerPaint);
        }else if (leftWhite == 0){
            rectProgressRight.left = leftWhite + margin;
            canvas.drawRoundRect(rectProgressRight, mDefaultTempHeight / 2, mDefaultTempHeight / 2, mPaintRight);
        }else if (rightWhite == 0){
            rectProgressLeft.right = leftWhite - margin;
            canvas.drawRoundRect(rectProgressLeft, mDefaultTempHeight / 2, mDefaultTempHeight / 2, mPaint);
        }
    }

    /**
     * 单位转换
     * @param dip
     * @return
     */
    private int dipToPx(int dip) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

    /***
     * 设置最大的温度值
     * @param maxCount
     */
    public void setMaxCount(float maxCount) {
        this.maxCount = maxCount;
    }

    /***
     * 设置当前的温度
     * @param currentCount
     */
    public void setCurrentCount(float currentCount) {
        if (currentCount > maxCount) {
            this.currentCount = maxCount - 5;
        } else if (currentCount < 0f) {
            currentCount = 0f + 5;
        } else {
            this.currentCount = currentCount;
        }
        invalidate();
    }

    /**
     * 设置温度计厚度
     * @param height
     */
    public void setTempHeight(int height) {
        this.mDefaultTempHeight = height;
    }

    /**
     * 设置文字大小
     * @param textSize
     */
    public void setTextSize(int textSize) {
        this.mDefaultTextSize = textSize;
    }

    /**
     * 获取温度计最大刻度
     * @return
     */
    public float getMaxCount() {
        return maxCount;
    }

    /**
     * 获取当前刻度
     * @return
     */
    public float getCurrentCount() {
        return currentCount;
    }
}